package com.ssy.lingxi.pay.controller.feign;

import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.pay.api.common.PayChannelEnum;
import com.ssy.lingxi.pay.api.dto.request.*;
import com.ssy.lingxi.pay.model.assetAccount.*;
import com.ssy.lingxi.pay.model.dto.UrlLinkDTO;
import com.ssy.lingxi.pay.service.IWeChatPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author jw.chen
 * @version 2.0.0
 * @menu 微信支付内部接口
 * 微信支付
 * @date 2020/10/24
 */
@RestController
@RequestMapping("/pay/weChat")
public class WeChatFeignController extends BaseController {

    @Resource
    private IWeChatPayService weChatPayService;

    @Resource
    private HttpServletRequest request;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 订单支付
     *
     * @param mobilePayRequest 接口参数
     * @return 支付二维码链接
     */
    @RequestMapping(value = "/orderPay", method = RequestMethod.POST)
    public Wrapper<String> orderPay(@RequestBody @Valid MobilePayRequest mobilePayRequest) {
        Integer payChannel = mobilePayRequest.getPayChannel();
        log.error("==========payChannel:" + payChannel);
        PayResult payResult = null;
        if (payChannel == null || PayChannelEnum.Platform.getCode().equals(payChannel)) {
            payResult = weChatPayService.nativePay(mobilePayRequest, request);
            if (payResult.isSuccess()) {
                //返回支付路径
                Map<String, String> body = (Map<String, String>) payResult.getBody();
                return Wrapper.success(body.get("code_url"));
            }
        } else if (PayChannelEnum.APP.getCode().equals(payChannel)) {
            payResult = weChatPayService.appPay(mobilePayRequest, request);
            if (payResult.isSuccess()) {
                //返回支付路径
                Object body = payResult.getBody();
                return Wrapper.success(JSONUtil.toJsonStr(body));
            }
        } else if (PayChannelEnum.Applet.getCode().equals(payChannel)) {
            payResult = weChatPayService.jsAPIPay(mobilePayRequest, request);
            if (payResult.isSuccess()) {
                //返回支付路径
                Object body = payResult.getBody();
                return Wrapper.success(JSONUtil.toJsonStr(body));
            }
        } else if (PayChannelEnum.JsApi.getCode().equals(payChannel)) {
            payResult = weChatPayService.h5Pay(mobilePayRequest, request);
            if (payResult.isSuccess()) {
                //返回支付路径
                Object body = payResult.getBody();
                return Wrapper.success(JSONUtil.toJsonStr(body));
            }
        }
        return Wrapper.fail(payResult.getMessage());
    }

    /**
     * 查询订单
     *
     * @param weChatOrderQueryRequest
     * @return
     */
    @RequestMapping(value = "/orderQuery", method = RequestMethod.POST)
    public Wrapper<Object> orderQuery(@RequestBody WeChatOrderQueryRequest weChatOrderQueryRequest) {
        PayResult payResult = weChatPayService.orderQuery(weChatOrderQueryRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getBody());
        } else {
            return Wrapper.fail(payResult.getMessage());
        }
    }

    /**
     * 关闭订单
     *
     * @param weChatCloseOrderRequest
     * @return
     */
    @RequestMapping(value = "/closeOrder", method = RequestMethod.POST)
    public Wrapper<Object> closeOrder(@RequestBody WeChatCloseOrderRequest weChatCloseOrderRequest) {
        PayResult payResult = weChatPayService.closeOrder(weChatCloseOrderRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getBody());
        } else {
            return Wrapper.fail(payResult.getMessage());
        }
    }

    /**
     * 申请退款
     *
     * @param weChatRefundRequest
     * @return
     */
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public Wrapper<Object> refund(@RequestBody WeChatRefundRequest weChatRefundRequest) {
        PayResult payResult = weChatPayService.refund(weChatRefundRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getBody());
        } else {
            return Wrapper.fail(payResult.getMessage());
        }
    }

    /**
     * 查询退款
     *
     * @param weChatRefundQueryRequest
     * @return
     */
    @RequestMapping(value = "/refundQuery", method = RequestMethod.POST)
    public Wrapper<Object> refundQuery(@RequestBody WeChatRefundQueryRequest weChatRefundQueryRequest) {
        PayResult payResult = weChatPayService.refundQuery(weChatRefundQueryRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getBody());
        } else {
            return Wrapper.fail(payResult.getMessage());
        }
    }

}
